﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Citizen_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Event for control "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim data As New VSEntities
        Dim p As New VSModel.Citizen
        'DUC 10-09-2014 kiem tra Code va Name da ton tai chua
        Dim checkCode = (From q In data.Citizens Where q.Code = txtCode.Text.Trim() Select q.Code).FirstOrDefault
        If checkCode Is Nothing Then
            Dim checkName = (From q In data.Citizens Where q.Name = txtName.Text.Trim() Select q).FirstOrDefault
            If checkName Is Nothing Then
                Try

                    p.Code = txtCode.Text.Trim()
                    p.Name = txtName.Text.Trim()
                    p.Desription = txtDescription.Text.Trim()
                    p.Created = Date.Now
                    p.Creator = Session("UserName")
                    Dim t = (From h In data.Citizens Order By h.Sort Descending Select h).FirstOrDefault
                    If Not t Is Nothing Then
                        p.Sort = CInt(t.Sort.ToString) + 1
                    Else
                        p.Sort = 1
                    End If

                    data.Citizens.AddObject(p)
                    data.SaveChanges()
                    Insert_App_Log("Create Citizen:" & txtName.Text.Trim & "", Function_Name.Citizen, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("Alertbox('Thêm mới dữ liệu thành công.');window.location ='../../Page/Citizen/List.aspx?Id=" & Request("Id") & "';", Me.Page, True)

                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');window.location ='../../Page/Citizen/List.aspx';", Me.Page, True)
                End Try
            Else
                Excute_Javascript("Alertbox('Danh mục dân tộc đã tồn tại trong hệ thống.')", Me.Page, True)
            End If
        Else
            Excute_Javascript("Alertbox('Mã đã tồn tại trong hệ thống.')", Me.Page, True)
        End If
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
